package Negocios;

import CDB.PersonFactory;
import CDB.Subcategoria;
import java.awt.HeadlessException;
import java.util.List;
import javax.swing.JOptionPane;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

/**
 *
 * @author lightemp
 */
public class ControlSubcategory {

    private Session session;
    private Transaction transaction;
    private SessionFactory sessionFactory;

    public List selectAll() {
        List listSubCategory;
        try {
            session = null;
            sessionFactory = PersonFactory.getSessionFactory();
            session = sessionFactory.openSession();
            try {
                listSubCategory = session.createQuery("from Subcategoria s").list();
                return listSubCategory;
            } catch (HibernateException e) {
                JOptionPane.showMessageDialog(null, "error: " + e, "Ha ocurrido un error", 0);
                return null;
            }
        } catch (HibernateException | HeadlessException e) {
            JOptionPane.showMessageDialog(null, "error: " + e, "Ha ocurrido un error", 0);
            return null;
        } finally {
            session.close();
        }
    }

    public Subcategoria selectOneByName(String nombre) {
        Subcategoria subcategoria = null;
        try {
            Session session = createSession();
            subcategoria = (Subcategoria) session.createQuery("from Subcategoria s where s.nombre='" + nombre + "'").list().get(0);
            session.close();
        } catch (IndexOutOfBoundsException ex) {
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "error: " + e, "Ha ocurrido un error", 0);
        }
        return subcategoria;
    }

    public Subcategoria selectOne(int id) {
        Subcategoria subcategoria = null;
        try {
            Session session = createSession();
            subcategoria = (Subcategoria) session.createQuery("from Subcategoria s where s.id='" + id + "'").list().get(0);
            session.close();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "error: " + e, "Ha ocurrido un error", 0);
        }
        return subcategoria;
    }

    public void add(String nombre) {
        try {
            iniciaSF();
            Subcategoria subcategoria = new Subcategoria();
            //Creando un objeto
            subcategoria.setNombre(nombre);
            session.save(subcategoria);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "error: " + e, "Ha ocurrido un error", 0);
        } finally {
            cierraSF();
        }
    }

    public void Update(int idSubCategoria, String nombre) {
        try {
            iniciaSF();

            Subcategoria subcategoria = (Subcategoria) session.load(Subcategoria.class,
                    new Integer(idSubCategoria));
            subcategoria.setNombre(nombre);
            session.update(subcategoria);
        } catch (HibernateException e) {
            JOptionPane.showMessageDialog(null, "error: " + e, "Ha ocurrido un error", 0);
        } finally {
            cierraSF();
        }
    }

    public void delete(int idSubcategoria) {
        try {
            iniciaSF();
            Subcategoria subcategoria = (Subcategoria) session.load(Subcategoria.class,
                    new Integer(idSubcategoria));
            session.delete(subcategoria);
        } catch (HibernateException e) {
            JOptionPane.showMessageDialog(null, "error: " + e, "Ha ocurrido un error", 0);
        } finally {
            cierraSF();
        }

    }

    public void deleteBySubcategoria(Subcategoria subcategoria) {
        try {
            iniciaSF();
            session.delete(subcategoria);
        } catch (HibernateException e) {
            JOptionPane.showMessageDialog(null, "error: " + e, "Ha ocurrido un error", 0);
        } finally {
            cierraSF();
        }

    }

    public void iniciaSF() {
        try {
            session = null;
            sessionFactory = PersonFactory.getSessionFactory();
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();
        } catch (HibernateException e) {
            JOptionPane.showMessageDialog(null, "error: " + e + "\n"
                    + "En el controlador de subcategoria.", "Error al iniciar Session",
                    0);
        }

    }

    public void cierraSF() {
        try {
            transaction.commit();
            session.close();
        } catch (HibernateException e) {
            JOptionPane.showMessageDialog(null, "error: " + e + "\n"
                    + "En el controlador de subcategoria.", "Error al cerrar Session",
                    0);
        }
    }

    public Session createSession() {

        Session session = null;
        try {
            //creando la nueva session para usar hibernate
            sessionFactory = PersonFactory.getSessionFactory();
            session = sessionFactory.openSession();

        } catch (HibernateException e) {
            JOptionPane.showMessageDialog(null, "error: " + e + "\n"
                    + "En el controlador de subcategoria.", "Error al crear Session",
                    0);
            session.close();
        }
        return session;
    }
}